/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kamus.manager;

import com.kamus.DB;
import com.kamus.model.Kosakata;
import com.mysql.jdbc.Statement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class KosakataGaulMgr extends BaseMgr{

    private static KosakataGaulMgr instance;

    public KosakataGaulMgr() {
        connection = getConnection();
    }

    public static KosakataGaulMgr getInstance() {
        if (instance == null) {
            instance = new KosakataGaulMgr();
        }
        return instance;
    }

    public int cekOrInsert(String kata) {
        int id = -1;
        id = cekKata(kata);
        if (id == -1) {
            id = insertKata(new Kosakata(kata));
        }
        return id;
    }

    public int cekKata(String kata) {
        int id = -1;
        cekConnection();
        try {
            psmnt = connection.prepareStatement("select ID,KOSAKATA from KOSAKATA_GAUL where KOSAKATA=?");
            psmnt.setString(1, kata);
            rs = psmnt.executeQuery();
            if (rs.next()) {
                id = rs.getInt("ID");
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    public int insertKata(Kosakata kata) {
        int id = -1;
        cekConnection();
        try {
            psmnt.executeUpdate("insert into KOSAKATA_GAUL(KOSAKATA) values('" + kata.getKata() + "')", Statement.RETURN_GENERATED_KEYS);
            rs = psmnt.getGeneratedKeys();
            if (rs.next()) {
                id = rs.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    public void deleteKata(int id) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("delete from KOSAKATA_GAUL where ID=?");
            psmnt.setInt(1, id);
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<Kosakata> getKatas() {
        cekConnection();
        ArrayList<Kosakata> list = new ArrayList<Kosakata>();
        Kosakata kata;
        try {
            psmnt = connection.prepareStatement("select * from KOSAKATA_GAUL order by KOSAKATA");
            rs = psmnt.executeQuery();
            while (rs.next()) {
                kata = new Kosakata();
                kata.setId(rs.getInt("id"));
                kata.setKata(rs.getString("kosakata"));
                list.add(kata);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public void updateKata(Kosakata kata) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("update KOSAKATA_GAUL set KOSAKATA=? where ID= ?");
            psmnt.setString(1, kata.getKata());
            psmnt.setInt(2, kata.getId());
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
